﻿Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Text
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Text.RegularExpressions

Namespace Active.Modules.Forums.Controls
    <ToolboxData("<{0}:TopicDisplay runat=server></{0}:TopicDisplay>")> _
    Public Class TopicDisplay
        Inherits ControlsBase
#Region "Private Members"
        Private bRead As Boolean = True
        Private bReply As Boolean = True
        Private bLocked As Boolean = False
        Private PageSize As Integer = 20
        Private RowIndex As Integer = 0

#End Region
#Region "Protected Members"
        Protected WithEvents cbTopicActions As New Active.Modules.Forums.Controls.Callback
        Protected WithEvents cbTopicLoader As New Active.Modules.Forums.Controls.Callback
#End Region
#Region "Event Handlers"
        Private Sub TopicDisplay_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
            Dim MainSettings As SettingsInfo = DataCache.MainSettings(ControlConfig.InstanceId)
            PageSize = MainSettings.PageSize
            Dim sTemp As String = String.Empty
            If Not ControlConfig Is Nothing Then
                Dim obj As Object = DataCache.CacheRetrieve(ControlConfig.InstanceId & CurrentView)
                If obj Is Nothing Then
                    sTemp = ParseTemplate()
                Else
                    sTemp = CType(obj, String)
                End If
                If sTemp.Contains("[NOPAGING]") Then
                    RowIndex = 0
                    PageSize = Integer.MaxValue
                    sTemp = sTemp.Replace("[NOPAGING]", String.Empty)
                End If
                sTemp = Utilities.ParseTokenConfig(sTemp, "topic", ControlConfig)

                Dim Subject As String = String.Empty
                Dim Body As String = String.Empty
                Dim CreateDate As Date = Nothing
                Dim EditDate As Date = Nothing
                Dim Tags As String = String.Empty
                Dim AuthorRoles As String = String.Empty
                Dim IPAddress As String = String.Empty
                Dim LastPostDate As Date = Nothing
                Dim LastPostData As String = String.Empty
                Dim tAuthor As UserProfileInfo = Nothing
                Dim ReplyCount As Integer = 0
                Dim tc As New Data.Topics
                Dim rowCount As Integer = 0
                Dim nextTopic As Integer = 0
                Dim prevTopic As Integer = 0
                Dim statusId As Integer = -1
                Dim topicRating As Double = 0
                Dim dr As IDataReader = tc.TopicForDisplay(ControlConfig.SiteId, ControlConfig.InstanceId, -1, TopicId, UserId, RowIndex, PageSize, "ASC")
                While dr.Read
                    ForumId = Integer.Parse(dr("ForumId").ToString)
                    Subject = dr("Subject").ToString
                    Body = dr("Body").ToString
                    CreateDate = CDate(dr("DateCreated").ToString)
                    EditDate = CDate(dr("DateUpdated").ToString)
                    Tags = dr("Tags").ToString
                    IPAddress = dr("IPAddress").ToString
                    LastPostDate = Date.Parse(Utilities.GetDate(Date.Parse(dr("LastPostDate").ToString), ControlConfig.InstanceId))
                    LastPostData = dr("LastPostData").ToString
                    ReplyCount = Integer.Parse(dr("ReplyCount").ToString)
                    nextTopic = Integer.Parse(dr("NextTopic").ToString)
                    prevTopic = Integer.Parse(dr("PrevTopic").ToString)
                    topicRating = Double.Parse(dr("TopicRating").ToString)
                    Dim profile As New UserProfileInfo
                    With profile
                        .UserID = Integer.Parse(dr("AuthorId").ToString)
                        If String.IsNullOrEmpty(dr("DisplayName").ToString) Then
                            .DisplayName = dr("AuthorName").ToString
                        Else
                            .DisplayName = dr("DisplayName").ToString
                        End If
                        .FirstName = dr("FirstName").ToString
                        .LastName = dr("LastName").ToString
                        .Username = dr("Username").ToString
                        .UserCaption = dr("UserCaption").ToString
                        .AnswerCount = Integer.Parse(dr("AnswerCount").ToString)
                        .AOL = dr("AOL").ToString
                        .Avatar = dr("Avatar").ToString
                        .AvatarType = CType(Integer.Parse(dr("AvatarType").ToString), AvatarTypes)
                        .DateCreated = Date.Parse(Utilities.GetDate(Date.Parse(dr("UserDateCreated").ToString), ControlConfig.InstanceId))
                        .DateLastActivity = Date.Parse(Utilities.GetDate(Date.Parse(dr("DateLastActivity").ToString), ControlConfig.InstanceId))
                        .DateLastPost = Date.Parse(Utilities.GetDate(Date.Parse(dr("DateLastPost").ToString), ControlConfig.InstanceId))
                        If Not String.IsNullOrEmpty(dr("UserDateUpdated").ToString) Then
                            .DateUpdated = Date.Parse(Utilities.GetDate(Date.Parse(dr("UserDateUpdated").ToString), ControlConfig.InstanceId))
                        End If
                        .Interests = dr("Interests").ToString
                        .IsUserOnline = Boolean.Parse(dr("IsUserOnline").ToString)
                        .Location = dr("Location").ToString
                        .MSN = dr("MSN").ToString
                        .Occupation = dr("Occupation").ToString
                        .TopicCount = Integer.Parse(dr("UserTopicCount").ToString)
                        .ReplyCount = Integer.Parse(dr("UserReplyCount").ToString)
                        .RewardPoints = Integer.Parse(dr("RewardPoints").ToString)
                        .Roles = dr("UserRoles").ToString
                        .Signature = dr("Signature").ToString
                        .TrustLevel = Integer.Parse(dr("TrustLevel").ToString)
                        .WebSite = dr("WebSite").ToString
                        .Yahoo = dr("Yahoo").ToString
                    End With
                    tAuthor = profile
                    If DataPageId = 1 Then

                        sTemp = ParseTopic(sTemp, Subject, CreateDate, Body, Tags, CStr(EditDate), IPAddress, ForumUser, rowCount)
                        rowCount += 1
                    Else
                        sTemp = TemplateUtils.ReplaceSubSection(sTemp, String.Empty, "[TOPIC]", "[/TOPIC]")
                    End If
                End While
                If ForumInfo Is Nothing Then
                    Dim fc As New ForumController
                    Dim fi As Forum = Nothing
                    fi = fc.Forums_Get(ForumId, UserId, True, True)
                    ForumInfo = fi
                End If
                sTemp = sTemp.Replace("[FORUMID]", ForumId.ToString)
                sTemp = sTemp.Replace("[FORUMNAME]", ForumInfo.ForumName)
                sTemp = sTemp.Replace("[TOPICID]", TopicId.ToString)
                sTemp = sTemp.Replace("[CREATEROLES]", ForumInfo.Security.Create)
                sTemp = sTemp.Replace("[USERROLES]", ForumUser.UserRoles)
                sTemp = sTemp.Replace("[THEMEPATH]", ThemePath)
                sTemp = sTemp.Replace("[SUBJECT]", Subject)
                sTemp = sTemp.Replace("[PAGEID]", PageId.ToString)
                sTemp = sTemp.Replace("[REPLYROLES]", ForumInfo.Security.Reply)
                sTemp = sTemp.Replace("AF:SECURITY:MODROLES]", "AF:SECURITY:MODROLES:" & ForumInfo.Security.ModApprove & "]")
                sTemp = sTemp.Replace("AF:SECURITY:MODAPPROVE]", "AF:SECURITY:MODAPPROVE:" & ForumInfo.Security.ModApprove & "]")
                sTemp = sTemp.Replace("AF:SECURITY:DELETE]", "AF:SECURITY:DELETE:" & ForumInfo.Security.Delete & ForumInfo.Security.ModDelete & "]")
                sTemp = sTemp.Replace("AF:SECURITY:EDIT]", "AF:SECURITY:EDIT:" & ForumInfo.Security.Edit & ForumInfo.Security.ModEdit & "]")
                sTemp = sTemp.Replace("AF:SECURITY:LOCK]", "AF:SECURITY:LOCK:" & ForumInfo.Security.Lock & ForumInfo.Security.ModLock & "]")
                sTemp = sTemp.Replace("AF:SECURITY:MOVE]", "AF:SECURITY:MOVE:" & ForumInfo.Security.ModMove & "]")
                sTemp = sTemp.Replace("AF:SECURITY:PIN]", "AF:SECURITY:PIN:" & ForumInfo.Security.Pin & ForumInfo.Security.ModPin & "]")
                sTemp = sTemp.Replace("AF:SECURITY:SPLIT]", "AF:SECURITY:SPLIT:" & ForumInfo.Security.ModSplit & "]")
                sTemp = sTemp.Replace("AF:SECURITY:REPLY]", "AF:SECURITY:REPLY:" & ForumInfo.Security.Reply & "]")
                If LastPostDate = Nothing Then
                    LastPostDate = CreateDate
                End If
                Dim LastPostAuthor As String = String.Empty
                If ((bRead And tAuthor.UserID = Me.UserId)) And statusId >= 0 Then
                    sTemp = sTemp.Replace("[AF:CONTROL:STATUS]", "<asp:placeholder id=""plhStatus"" runat=""server"" />")
                    sTemp = sTemp.Replace("[AF:CONTROL:STATUSICON]", "<img alt=""[RESX:PostStatus" & statusId.ToString & "]"" src=""" & ThemePath & "status" & statusId.ToString & ".png"" />")
                ElseIf statusId >= 0 Then
                    sTemp = sTemp.Replace("[AF:CONTROL:STATUS]", String.Empty)
                    sTemp = sTemp.Replace("[AF:CONTROL:STATUSICON]", "<img alt=""[RESX:PostStatus" & statusId.ToString & "]"" src=""" & ThemePath & "status" & statusId.ToString & ".png"" />")
                Else
                    sTemp = sTemp.Replace("[AF:CONTROL:STATUS]", String.Empty)
                    sTemp = sTemp.Replace("[AF:CONTROL:STATUSICON]", String.Empty)
                    sTemp = sTemp.Replace("[ACTIONS:ANSWER]", String.Empty)
                End If
                If String.IsNullOrEmpty(LastPostData) Then
                    LastPostAuthor = UserProfiles.GetDisplayName(ControlConfig.InstanceId, MainSettings.ProfileVisibility, False, tAuthor.UserID, MainSettings.UserNameDisplay, MainSettings.DisableUserProfiles, tAuthor.Username, tAuthor.FirstName, tAuthor.LastName, tAuthor.DisplayName)
                Else
                    Dim la As New Author
                    Dim xDoc As New System.Xml.XmlDocument
                    xDoc.LoadXml(LastPostData)
                    Dim xNode As System.Xml.XmlNode = xDoc.SelectSingleNode("//root/lastpost")
                    If Not xNode Is Nothing Then
                        With la
                            .AuthorId = Integer.Parse(xNode.Item("authorid").InnerText.ToString)
                            .DisplayName = xNode.Item("displayname").InnerText
                            .FirstName = xNode.Item("firstname").InnerText
                            .LastName = xNode.Item("lastname").InnerText
                            .Username = xNode.Item("username").InnerText
                        End With
                    End If
                    LastPostAuthor = UserProfiles.GetDisplayName(ControlConfig.InstanceId, MainSettings.ProfileVisibility, False, la.AuthorId, MainSettings.UserNameDisplay, MainSettings.DisableUserProfiles, la.Username, la.FirstName, la.LastName, la.DisplayName)
                End If
                'TODO:Fix LastPostDate Format
                sTemp = sTemp.Replace("[AF:LABEL:LastPostDate]", LastPostDate.ToString)
                sTemp = sTemp.Replace("[AF:LABEL:LastPostAuthor]", LastPostAuthor)
                sTemp = sTemp.Replace("[AF:LABEL:ReplyCount]", ReplyCount.ToString)
                Dim sURL As String = "<a rel=""nofollow"" href=""" & Utilities.NavigateUrl(PageId, "", ParamKeys.ForumId & "=" & ForumId, ParamKeys.ViewType & "=" & Views.Topic, ParamKeys.TopicId & "=" & TopicId, "mid=" & ControlConfig.InstanceId.ToString, "dnnprintmode=true") & "?skinsrc=" & HttpUtility.UrlEncode("[G]" & DotNetNuke.UI.Skins.SkinInfo.RootSkin & "/" & DotNetNuke.Common.glbHostSkinFolder & "/" & "No Skin") & "&amp;containersrc=" & HttpUtility.UrlEncode("[G]" & DotNetNuke.UI.Skins.SkinInfo.RootContainer & "/" & DotNetNuke.Common.glbHostSkinFolder & "/" & "No Container") & """ target=""_blank"">"
                sURL &= "<img src=""" & ThemePath & "images/spacer.gif"" alt=""[RESX:PrinterFriendly]"" class=""aficon aficonprint"" /></a>"
                sTemp = sTemp.Replace("[AF:CONTROL:PRINTER]", sURL)
                If HttpContext.Current.Request.IsAuthenticated Then
                    sURL = Utilities.NavigateUrl(PageId, "", New String() {ParamKeys.ViewType & "=sendto", ParamKeys.ForumId & "=" & ForumId, ParamKeys.TopicId & "=" & TopicId})
                    sTemp = sTemp.Replace("[AF:CONTROL:EMAIL]", "<a href=""" & sURL & """ rel=""nofollow""><img src=""" & ThemePath & "images/spacer.gif"" class=""aficon aficonemail"" alt=""[RESX:EmailThis]"" /></a>")
                Else
                    sTemp = sTemp.Replace("[AF:CONTROL:EMAIL]", String.Empty)
                End If
                If ForumInfo.AllowRSS Then
                    Dim Url As String
                    Url = DotNetNuke.Common.AddHTTP(DotNetNuke.Common.GetDomainName(HttpContext.Current.Request)) & "/DesktopModules/ActiveForums/feeds.aspx?portalid=" & ControlConfig.SiteId & "&forumid=" & ForumId & "&tabid=" & PageId & "&moduleid=" & ControlConfig.InstanceId
                    sTemp = sTemp.Replace("[RSSLINK]", "<a href=""" & Url & """><img src=""" & ThemePath & "images/rss.png"" runat=server border=""0"" alt=""[RESX:RSS]"" /></a>")
                Else
                    sTemp = sTemp.Replace("[RSSLINK]", String.Empty)
                End If
                If nextTopic = 0 Then
                    sTemp = sTemp.Replace("[NEXTTOPICID]", String.Empty)
                    sTemp = sTemp.Replace("[HASNEXTTOPIC]", "False")
                Else
                    sTemp = sTemp.Replace("[NEXTTOPICID]", nextTopic.ToString)
                    sTemp = sTemp.Replace("[HASNEXTTOPIC]", "True")
                End If
                If prevTopic = 0 Then
                    sTemp = sTemp.Replace("[PREVTOPICID]", String.Empty)
                    sTemp = sTemp.Replace("[HASPREVTOPIC]", "False")
                Else
                    sTemp = sTemp.Replace("[PREVTOPICID]", prevTopic.ToString)
                    sTemp = sTemp.Replace("[HASPREVTOPIC]", "True")
                End If
                dr.NextResult()
                'Process Replies
                Dim sb As New StringBuilder
                sb.Append(String.Empty)
                Dim replyId As Integer = 0
                While dr.Read()
                    Subject = dr("Subject").ToString
                    Body = dr("Body").ToString
                    CreateDate = CDate(dr("DateCreated").ToString)
                    EditDate = CDate(dr("DateUpdated").ToString)
                    IPAddress = dr("IPAddress").ToString
                    replyId = Integer.Parse(dr("ReplyId").ToString)
                    Dim profile As New UserProfileInfo
                    With profile
                        .UserID = Integer.Parse(dr("AuthorId").ToString)
                        If String.IsNullOrEmpty(dr("DisplayName").ToString) Then
                            .DisplayName = dr("AuthorName").ToString
                        Else
                            .DisplayName = dr("DisplayName").ToString
                        End If
                        .FirstName = dr("FirstName").ToString
                        .LastName = dr("LastName").ToString
                        .Username = dr("Username").ToString
                        .UserCaption = dr("UserCaption").ToString
                        .AnswerCount = Integer.Parse(dr("AnswerCount").ToString)
                        .AOL = dr("AOL").ToString
                        .Avatar = dr("Avatar").ToString
                        .AvatarType = CType(Integer.Parse(dr("AvatarType").ToString), AvatarTypes)
                        .DateCreated = Date.Parse(Utilities.GetDate(Date.Parse(dr("UserDateCreated").ToString), ControlConfig.InstanceId))
                        .DateLastActivity = Date.Parse(Utilities.GetDate(Date.Parse(dr("DateLastActivity").ToString), ControlConfig.InstanceId))
                        If Not String.IsNullOrEmpty(dr("DateLastPost").ToString) Then
                            .DateLastPost = Date.Parse(Utilities.GetDate(Date.Parse(dr("DateLastPost").ToString), ControlConfig.InstanceId))
                        End If

                        If Not String.IsNullOrEmpty(dr("UserDateUpdated").ToString) Then
                            .DateUpdated = Date.Parse(Utilities.GetDate(Date.Parse(dr("UserDateUpdated").ToString), ControlConfig.InstanceId))
                        End If
                        .Interests = dr("Interests").ToString
                        .IsUserOnline = Boolean.Parse(dr("IsUserOnline").ToString)
                        .Location = dr("Location").ToString
                        .MSN = dr("MSN").ToString
                        .Occupation = dr("Occupation").ToString
                        .TopicCount = Integer.Parse(dr("UserTopicCount").ToString)
                        .ReplyCount = Integer.Parse(dr("UserReplyCount").ToString)
                        .RewardPoints = Integer.Parse(dr("RewardPoints").ToString)
                        .Roles = dr("UserRoles").ToString
                        .Signature = dr("Signature").ToString
                        .TrustLevel = Integer.Parse(dr("TrustLevel").ToString)
                        .WebSite = dr("WebSite").ToString
                        .Yahoo = dr("Yahoo").ToString
                    End With
                    sb.Append(ParseReply(sTemp, replyId, Subject, CreateDate, Body, CStr(EditDate), IPAddress, ForumUser, rowCount))
                    rowCount += 1
                End While
                dr.Close()
                sTemp = TemplateUtils.ReplaceSubSection(sTemp, sb.ToString, "[REPLIES]", "[/REPLIES]")
                sTemp = Utilities.LocalizeControl(sTemp)
                sTemp = sTemp.Replace("[TOPICID]", TopicId.ToString)
                sTemp = sTemp.Replace("[GROUPNAME]", ForumInfo.GroupName)
                sTemp = sTemp.Replace("[FORUMNAME]", ForumInfo.ForumName)
                sTemp = sTemp.Replace("[TOPICRATING]", topicRating.ToString)
                sTemp = sTemp.Replace("[CURRENTUSERID]", UserId.ToString)

                sTemp = Utilities.ParseSecurityTokens(sTemp, ForumUser.UserRoles)
                If Not sTemp.Contains(Globals.ControlRegisterAFTag) Then
                    sTemp = Globals.ControlRegisterAFTag & sTemp
                End If
                Dim ctl As Control = Page.ParseControl(sTemp)
                LinkControls(ctl.Controls)
                Me.Controls.Add(ctl)
            End If
        End Sub
#End Region
#Region "Private Methods"
        Private Function ParseTemplate() As String
            Dim sOut As String = DisplayTemplate
            'sOut = ParseForumData(sOut)
            sOut = ParseTopicControls(sOut)
            sOut = sOut.Replace("[AF:CONTROL:CALLBACK]", String.Empty)
            sOut = sOut.Replace("[/AF:CONTROL:CALLBACK]", String.Empty)

            DataCache.CacheStore(ControlConfig.InstanceId & CurrentView, sOut)
            Return sOut
        End Function
        Private Function ParseTopic(ByVal template As String, ByVal Subject As String, ByVal PostDate As Date, ByVal Body As String, ByVal Tags As String, ByVal EditDate As String, ByVal IPAddress As String, ByVal profile As User, ByVal RowCount As Integer) As String

            Dim t As String = TemplateUtils.GetTemplateSection(template, "[TOPIC]", "[/TOPIC]")
            t = Utilities.ParseTokenConfig(t, "topic", ControlConfig)
            t = t.Replace("[SUBJECT]", Subject)
            t = t.Replace("[BODY]", Body)
            t = t.Replace("[POSTDATE]", Utilities.GetDate(PostDate, ControlConfig.InstanceId))
            t = t.Replace("[DATECREATED]", Utilities.GetDate(PostDate, ControlConfig.InstanceId))
            t = t.Replace("[POSTID]", TopicId.ToString)
            t = t.Replace("[FORUMID]", ForumId.ToString)

            If PostDate = CDate(EditDate) Or CDate(EditDate) = Date.MinValue Or CDate(EditDate) = Utilities.NullDate Then
                t = t.Replace("[MODEDITDATE]", String.Empty)
            Else
                t = t.Replace("[MODEDITDATE]", Utilities.GetDate(CDate(EditDate), ControlConfig.InstanceId))
            End If
            If String.IsNullOrEmpty(Tags) Then
                t = TemplateUtils.ReplaceSubSection(t, String.Empty, "[AF:CONTROL:TAGS]", "[/AF:CONTROL:TAGS]")
            Else
                t = t.Replace("[AF:LABEL:TAGS]", Tags)
            End If
            t = TemplateUtils.ParseProfileTemplate(t, ForumUser, ControlConfig.SiteId, ControlConfig.InstanceId, ThemePath, CurrentUserTypes.Auth, True, ControlConfig.User.PrefBlockAvatars, ControlConfig.User.PrefBlockSignatures, IPAddress)
            t = t.Replace("[ATTACHMENTS]", String.Empty)
            If RowCount Mod 2 = 0 Then
                t = t.Replace("[POSTINFOCSS]", "afpostinfo1")
                t = t.Replace("[POSTTOPICCSS]", "afpostreply1")
                t = t.Replace("[POSTREPLYCSS]", "afpostreply1")
            Else
                t = t.Replace("[POSTTOPICCSS]", "afpostreply2")
                t = t.Replace("[POSTINFOCSS]", "afpostinfo2")
                t = t.Replace("[POSTREPLYCSS]", "afpostreply2")
            End If
            template = TemplateUtils.ReplaceSubSection(template, t, "[TOPIC]", "[/TOPIC]")
            Return template
        End Function
        Private Function ParseReply(ByVal template As String, ByVal PostId As Integer, ByVal Subject As String, ByVal PostDate As Date, ByVal Body As String, ByVal EditDate As String, ByVal IPAddress As String, ByVal profile As User, ByVal RowCount As Integer) As String
            Dim t As String = TemplateUtils.GetTemplateSection(template, "[REPLIES]", "[/REPLIES]")
            t = Utilities.ParseTokenConfig(t, "topic", ControlConfig)
            t = t.Replace("[SUBJECT]", Subject)
            t = t.Replace("[BODY]", Body)
            t = t.Replace("[POSTDATE]", Utilities.GetDate(PostDate, ControlConfig.InstanceId))
            t = t.Replace("[DATECREATED]", Utilities.GetDate(PostDate, ControlConfig.InstanceId))
            t = t.Replace("[POSTID]", PostId.ToString)
            t = t.Replace("[FORUMID]", ForumId.ToString)
            t = t.Replace("[TOPICID]", TopicId.ToString)

            If PostDate = CDate(EditDate) Or CDate(EditDate) = Date.MinValue Or CDate(EditDate) = Utilities.NullDate Then
                t = t.Replace("[MODEDITDATE]", String.Empty)
            Else
                t = t.Replace("[MODEDITDATE]", Utilities.GetDate(CDate(EditDate), ControlConfig.InstanceId))
            End If
            t = t.Replace("[ATTACHMENTS]", String.Empty)
            t = TemplateUtils.ParseProfileTemplate(t, profile, ControlConfig.SiteId, ControlConfig.InstanceId, ThemePath, CurrentUserTypes.Auth, True, ControlConfig.User.PrefBlockAvatars, ControlConfig.User.PrefBlockSignatures, IPAddress)
            If RowCount Mod 2 = 0 Then
                t = t.Replace("[POSTINFOCSS]", "afpostinfo1")
                t = t.Replace("[POSTTOPICCSS]", "afpostreply1")
                t = t.Replace("[POSTREPLYCSS]", "afpostreply1")
            Else
                t = t.Replace("[POSTTOPICCSS]", "afpostreply2")
                t = t.Replace("[POSTINFOCSS]", "afpostinfo2")
                t = t.Replace("[POSTREPLYCSS]", "afpostreply2")
            End If
            Return t
        End Function
        Private Function ParseTopicControls(ByVal template As String) As String
            template = template.Replace("[AF:CONTROL:ADDTHIS]", "<af:addthis runat=""server"" AddThisId=""" & MainSettings.AddThisAccount & """ Title=""[SUBJECT]"" />")

            template = template.Replace("[NOPAGING]", "[NOPAGING]<script type=""text/javascript"">afpagesize=" & Integer.MaxValue & ";</script>")
            template = ParseBanner(template)
            If bLocked Then
                template = template.Replace("[ADDREPLY]", "<span class=""afnormal"">[RESX:TopicLocked]</span>")
                template = template.Replace("[QUICKREPLY]", String.Empty)
            End If
            'template = template.Replace("[ADDREPLY]", "<af:imagebutton runat='server' imagelocation='LEFT' cssclass='ambutton' PostBack='false' PageId='[PAGEID]' Params='[PARAMKEYS:VIEWTYPE]=topicreply,[PARAMKEYS:FORUMID]=[FORUMID],[PARAMKEYS:TOPICID]=[TOPICID]' text='[RESX:AddReply]' imageurl='[THEMEPATH]add16.png' NotAuthText='[RESX:NotAuthorizedReply]' AuthRoles='[REPLYROLES]' NotAuthCSS='amnormal' UserRoles='[USERROLES]' />")
            'template = template.Replace("[QUICKREPLY]", "<af:quickreply runat='server' Subject=""[SUBJECT]"" templatefile='quickreply.ascx' AuthRoles='[REPLYROLES]' UserRoles='[USERROLES]' />")

            Return template
        End Function
        Private Sub LinkControls(ByVal ctrls As ControlCollection)
            For Each ctrl As Control In ctrls
                If TypeOf ctrl Is Controls.ControlsBase Then
                    DirectCast(ctrl, Controls.ControlsBase).ControlConfig = Me.ControlConfig
                    DirectCast(ctrl, Controls.ControlsBase).ForumData = Me.ForumData
                    DirectCast(ctrl, Controls.ControlsBase).ForumId = Me.ForumId
                End If
                If ctrl.Controls.Count > 0 Then
                    LinkControls(ctrl.Controls)
                End If
            Next
        End Sub
        Private Function ParseBanner(ByVal template As String) As String
#If SKU = "PROFESSIONAL" Or SKU = "ENTERPRISE" Then
            If template.Contains("[BANNER") Then
                Dim bannerCount As Integer = 1
                template = "<%@ Register TagPrefix=""dnn"" TagName=""BANNER"" Src=""~/Admin/Skins/Banner.ascx"" %>" & template
                template = template.Replace("[BANNER]", "<dnn:BANNER runat=""server"" id=""dnnBANNER" & bannerCount & """ BannerTypeId=""-1"" GroupName=""FORUMS"" EnableViewState=""False"" />")
                Dim pattern As String = "(\[BANNER:(.+?)\])"
                Dim regExp As New Regex(pattern)
                Dim matches As MatchCollection
                matches = regExp.Matches(template)
                Dim sBanner As String = "<dnn:BANNER runat=""server"" id=""dnnBANNER{0}"" BannerTypeId=""-1"" GroupName=""{1}"" EnableViewState=""False"" />"
                For Each match As Match In matches
                    Dim sReplace As String
                    bannerCount += 1
                    sReplace = String.Format(sBanner, bannerCount, match.Groups(2).Value)
                    template = regExp.Replace(template, sReplace, 1)
                Next match
            End If
#Else
            template = template.Replace("[BANNER]", String.Empty)
            template = template.Replace("<%@ Register TagPrefix=""dnn"" TagName=""BANNER"" Src=""~/Admin/Skins/Banner.ascx"" %>", String.Empty)
#End If
            Return template
        End Function
#End Region

    End Class

End Namespace
